package isp.apps.example.shoppingcart.domain;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ShoppingCartValidator
{
  public static final Pattern SEVEN_TWO_DECIMAL = Pattern.compile("[\\d]{1,7}+([\\.][\\d]{2}+)??");
  public static final Pattern DIGIT = Pattern.compile("\\d+");
  private static final Pattern NON_DIGIT_PATTERN = Pattern.compile("\\D");
  private static final Pattern PUNCTUATION_PATTERN = Pattern.compile("\\p{Punct}");
  private static final Pattern SPACE = Pattern.compile("\\p{Space}");
  private static final Pattern ALPHA_NUMERIC = Pattern.compile("\\p{Alnum}+");
  Matcher m;
  //private String fName, lName, address1, address2, city, state, zip, phone, ccType, ccNumber, ccSecurity, ccExpiration;
  String s = "";

  public ShoppingCartValidator()
  {
  }

  /**
   * Given an input string ccNumber, checkCCNumber calls the checkNumber method
   * with the initial string and the length of 16.
   * @param ccNumber the possible credit card number string
   * @return reformatted string if valid, or null of incorrectly formatted
   */

  public String checkBoolean(String value)
  {
    if (value==null)
       return null;
    value = value.trim();
    value = checkNumber(value, 1);
    if (value != null)
    {
        Integer t = new Integer(value);
      if (t.compareTo(new Integer(0)) == 0)
        return value;
      if (t.compareTo(new Integer(1)) == 0)
        return value;
      else return null;
    }
    else 
        return null;
  }

  /**Calls the checkNumber method and provides a length argument of 16.  If the input contains any punctuation characters,
   * these are stripped out along with trailing and leading spaces.  If non-number characters are left, then the method 
   * returns null.
   * @param ccNumber
   * @return
   */
  public String checkCCNumber(String ccNumber)
  {
    return checkNumber(ccNumber, 16);
  }

  /**Calls the checkNumber method and provides a length argument of 4.  If the input contains any punctuation characters,
   * these are stripped out along with trailing and leading spaces.  If non-number characters are left, then the method 
   * returns null.
   * @param ccExpiration
   * @return String
   */
  public String checkCCExpiration(String ccExpiration)
  {
    return checkNumber(ccExpiration, 4);
  }

  /**Calls the checkNumber method and provides a length argument of 3.  If the input contains any punctuation characters,
   * these are stripped out along with trailing and leading spaces.  If non-number characters are left, then the method 
   * returns null.
   * @param ccSecurity
   * @return String
   */
  public String checkCCSecurity(String ccSecurity)
  {
    return checkNumber(ccSecurity, 3);
  }

  /**Calls the checkNumber method and provides a length argument of 5.  If the input contains any punctuation characters,
   * these are stripped out along with trailing and leading spaces.  If non-number characters are left, then the method 
   * returns null.
   * @param zip
   * @return
   */
  public String checkZipCode(String zip)
  {
    return checkNumber(zip, 5);
  }


  /**Calls the CheckNumber method and provides a length argument of 10.  If the input contains any punctuation characters,
   * these are stripped out along with trailing and leading spaces.  If non-number characters are left, then the method 
   * returns null.
   * @param phone
   * @return
   */
  public String checkPhone(String phone)
  {
    return checkNumber(phone, 10);
  }

  /**Checks the input String to make sure it is not null or only space characters.  Returns a modified version with 
   * trailing and leaning spaces trimmed if needed.  If a space only string or a null string is passed in, the method 
   * returns null.
   * @param text
   * @return
   */
  public String checkText(String text)
  {
    if (text == null)
      return null;
    s = text.trim();
    if (s.equals(""))
      return null;
    else
      return s;
  }

  /**Checks the input String to see if it conforms to a valid currency representation.  Valid currency representations
   * are considered either a whole number up to 7 digits or a whole number up to 7 digit with a decimal point and two 
   * digits for the cents.  Returns null if any non-numeric values are included so the number may not be negative.
   * @param currency
   * @return
   */
  public String checkCurrency(String currency)
  {
    if (currency==null)
       return null;
    s = currency.trim();
    if (s.equals(""))
      return null;
    m = SEVEN_TWO_DECIMAL.matcher(s);
    if (!m.matches())
    return null;
    else
      return s;
  }

  /**
   * Returns a string of numbers if given a valid string of numbers.  Returns the null string if given an 
   * invalid string containing non-number characters.
   * @param number
   * @return s A string
   */
  public String checkNumber(String number)
  {
    if (number == null)
      return null;
    s = number.trim();
    if (s.equals(""))
      return null;
    m = NON_DIGIT_PATTERN.matcher(s);
    if (m.find())
        return null;
    else
        return s;
  }

  /**
   * Returns a string where all non-digit characters are stripped.  If the stripped string does not match the given 
   * int length, then the null string is returned, else the reformatted string is returned.
   * @param number
   * @param length
   * @return
   */
  public String checkNumber(String number, int length)
  {
    if ((length <= 0) || (number == null))
      return null;
    s = number.trim();
    m = PUNCTUATION_PATTERN.matcher(s);
    s = m.replaceAll("");
    s = s.trim();
    
    m = SPACE.matcher(s);
    s = m.replaceAll("");
    
    
    if (s.equals(""))
      return null;
    if (s.length() != length)
        return null;
    else 
        return checkNumber(s);
  }
  
  
  public String checkNegativeNumber(String number)
  {
    s = number.trim();
    m = PUNCTUATION_PATTERN.matcher(s);
    s = m.replaceAll("");
    s = s.trim();
    
    m = SPACE.matcher(s);
    s = m.replaceAll("");  
    if (number == null)
      return null;
    s = number.trim();
    if (s.equals(""))
      return null;
    m = NON_DIGIT_PATTERN.matcher(s);
    if (m.find())
        return null;
    else
        return s;
  }
  
  
  public String checkAlphaNumeric(String number)
  {
    if (number==null)
       return null;
    s = number.trim();
    m=ALPHA_NUMERIC.matcher(s);
    if (m.matches())
       return s;
    return null;
  }
  
}